package com.handinfo.game;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.Random;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.util.Tools;

public class RoleCombatSkill extends CombatSkill
{

	public static final int INDEX_COMBATSKILL_SKILL = 0;// 技能
	public static final int INDEX_COMBATSKILL_GENIUS = 1;// 天赋
	//
	public static final byte SKILL_LEARNED = 0;// 已学
	public static final byte SKILL_UNLEARNED = 1;// 未学x
	//
	public static final byte TYPE_SKILL_UNFORGET = 0;// 无法遗忘技能
	public static final byte TYPE_SKILL_CHANGECAREER_BEFORE = 1;// 一转前技能
	public static final byte TYPE_SKILL_CHANGECAREEN1 = 2;// 一转技能
	public static final byte TYPE_SKILL_CHANGECAREEN2 = 3;// 二转技能
	public static final byte TYPE_SKILL_GENIUS = 4;// 天赋

	public short m_id = 0; // 技能ID，非唯一标识（不同级别的某技能该ID不同）
	public short m_groupId ;//技能组Id，多个技能Id对应一个技能组Id
	public short m_id_nextLvl = 0; // 下一级技能的ID
	public byte m_type_skill;// 技能类别（瞬发，吟唱）
	public short m_iconId = 0; // iconId
	public short m_iconCoverId = 0; // icon蒙板Id
	public boolean m_canLearn = false; // 是否可学
//	public boolean m_isPassive = false; // 是否被动技能
	// public byte m_type = 0; //0：瞬发 1：吟唱 2：通道
	public byte m_targetType = 0; // 0：对敌 1：对友 2：对敌或对友 3：无需目标
	public String m_name; // 名称
	public byte m_level = 0; // 当前级别
	public byte m_levelMax = 0; // 最大级别
	// public short m_singActionId = 0; // 施放吟唱或者准备动作
	// public short m_singEffectId = 0; // 吟唱光效
	// public short m_castSrcActionId = 0; //施法者动作id
	// public short m_castSrcEffectId = 0; //施法者光效id
	// public short m_castTgtActionId = 0; //施法目标动作id
	// public short m_castTgtEffectId = 0; //施法目标光效id
	public long m_castSrcBufLimit = 0; // 施法者buf限制
	public long m_castTgtBufLimit = 0; // 施法目标buf限制
	public int m_consumption = 0; // 技能消耗
	public int m_range = 0; // 攻击范围
	public int m_cd = 0; // CD
	public int m_cd_counter = 0; // CD计数器
	public int m_publicCD = 0; // 公共CD
	public int m_singTime = 0; // 吟唱时间（一般用于吟唱法术）
	public int m_castingTime = 0; // 施法时间（一般用于通道法术）
	public boolean m_isLearn;// 0 已会，1未学
	public byte m_mode = 0;//技能释放需要mode值

	public boolean m_running = false;

	/**
	 * 加载
	 * 
	 * @param dis
	 * @throws IOException
	 */
	public synchronized void read(DataInputStream dis) throws IOException
	{
		m_id = dis.readShort();
		m_groupId = dis.readShort() ;
		m_id_nextLvl = dis.readShort();
		if (m_id == -1) {
			return;
		}
		m_type_skill = dis.readByte();
		m_isLearn = dis.readByte() == 0 ? true : false;
		m_iconId = dis.readShort();//显示位置
		m_iconCoverId = dis.readShort();
		m_name = dis.readUTF();
		m_canLearn = dis.readByte() != 0;
		m_type = dis.readByte();
		m_targetType = dis.readByte();
		m_level = dis.readByte();
		m_levelMax = dis.readByte();
		m_singActionId = dis.readShort();
		m_singEffectId = dis.readShort();
		m_castSrcActionId = dis.readShort();
		m_castSrcEffectId = dis.readShort();
		m_castTgtActionId = dis.readShort();
		m_castTgtEffectId = dis.readShort();
		m_castSrcBufLimit = dis.readLong();
		m_castTgtBufLimit = dis.readLong();
		m_consumption = dis.readInt();
		m_cd = dis.readInt() * GameManager.FRAME_PER_SECOND;
		m_publicCD = dis.readInt() * GameManager.FRAME_PER_SECOND;
		m_castingTime = dis.readShort() * GameManager.FRAME_PER_SECOND;
		m_range = dis.readShort();
		m_mode = dis.readByte();
	}
	
	public static final RoleCombatSkill getTestRoleSkill(Random random){
		RoleCombatSkill skill = new RoleCombatSkill();
		skill.m_id = (short)Tools.getRandom(0, 1000000);
		skill.m_iconId = (short)Tools.getRandom(0, 80) ;
		skill.m_iconCoverId = (short)Tools.getRandom(0, 80);
		skill.m_name = "Skill " + (Tools.getRandom(0, 100));
		return skill ;
	}

	/**
	 * 释放
	 */
	public synchronized void dispose()
	{
		m_name = null;
	}

	/**
	 * 绘制图标
	 * 
	 * @param g
	 * @param x
	 * @param y
	 * @param anchor
	 */
	public synchronized void drawIcon(Graphics g, int x, int y, int anchor)
	{
		// ///游戏场景中用到的各图标均使用GameScene中的s_img_icon
		// if (m_iconId != -1) {
		// AbstractGameScreen.drawRegion(g, GameScene.s_img_icon, (m_iconId %
		// GameScene.SIZE_IMG_ICON[2])
		// * GameScene.SIZE_IMG_ICON[0], (m_iconId / GameScene.SIZE_IMG_ICON[2])
		// * GameScene.SIZE_IMG_ICON[1],
		// GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0, x, y,
		// anchor);
		// }
		// if (m_iconCoverId != -1) {
		// AbstractGameScreen.drawRegion(g, GameScene.s_img_icon_cover,
		// (m_iconCoverId % GameScene.SIZE_IMG_ICON[2])
		// * GameScene.SIZE_IMG_ICON[0], (m_iconCoverId /
		// GameScene.SIZE_IMG_ICON[2])
		// * GameScene.SIZE_IMG_ICON[1],
		// GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0, x, y,
		// anchor);
		// }
		drawIcon(g, x, y, anchor, m_iconId, m_iconCoverId);
	}

	public synchronized void drawIcon(Graphics g, int x, int y, int anchor, short m_iconId, short m_iconCoverId)
	{
		// ///游戏场景中用到的各图标均使用GameScene中的s_img_icon
		short iconId = (short) (m_iconId -1 ) ;
		if (iconId != -1) {
			AbstractGameScreen.drawRegion(g, GameScene.s_img_icon,
					(iconId % GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[0],
					(iconId / GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[1],
					GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0, x, y, anchor);
		}
		if (m_iconCoverId != -1) {
			short id = (short) (m_iconCoverId ) ;
			AbstractGameScreen.drawRegion(g, GameScene.s_img_icon_cover,
					(id % GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[0],
					(id / GameScene.SIZE_IMG_ICON[2]) * GameScene.SIZE_IMG_ICON[1],
					GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1], 0, x, y, anchor);
		}
		int clipX = g.getClipX();
		int clipY = g.getClipY();
		int clipW = g.getClipWidth();
		int clipH = g.getClipHeight();
		g.setClip(x, y, GameScene.SIZE_IMG_ICON[0], GameScene.SIZE_IMG_ICON[1]);
		// 绘制CD时的阴影
		int pCD_counter = GameManager.getInstance().m_role.m_publicCD_skill_counter;
		int pCD = GameManager.getInstance().m_role.m_publicCD_skill;
		if (pCD == 0 && m_cd == 0){
		} else {
			if (m_cd_counter > 0 || pCD_counter > 0) {
				if (m_cd_counter * pCD > m_cd * pCD_counter) {
					if (m_cd > 0) {
						Tools.fillArc(g, GameManager.COLOR_ACTOR_SHADOW,
								x + GameScene.SIZE_IMG_ICON[0] / 2,
								y + GameScene.SIZE_IMG_ICON[1] / 2,
								GameScene.SIZE_IMG_ICON[0] * 2,
								90 - (m_cd - m_cd_counter) * 360 / m_cd,
								-m_cd_counter * 360 / m_cd);
					}
				} else {
					if (pCD > 0) {
						Tools.fillArc(g, GameManager.COLOR_ACTOR_SHADOW, 
								x + GameScene.SIZE_IMG_ICON[0] / 2,
								y + GameScene.SIZE_IMG_ICON[1] / 2,
								GameScene.SIZE_IMG_ICON[0] * 2,
								90 - (pCD - pCD_counter) * 360 / pCD,
								-pCD_counter * 360 / pCD);
					}
				}
			}
		}
		g.setClip(clipX, clipY, clipW, clipH);
	}

	/**
	 * 技能冷却计时
	 */
	public void cooldown()
	{
		if (m_cd_counter > 0) {
			m_cd_counter--;
		}
	}
	
	/**
	 *是否适合放置在快捷栏
	 */
	public static boolean isFitforSet(RoleCombatSkill skill)
	{
		if (skill == null) {
			return false ;
		}
		if (!skill.m_isLearn) {
			return false;
		}
		if (skill.m_type == 3) {
			return false;
		}
		return true;
	}
	
	public boolean isPasstive(){
		return m_type == 3 ;
	}
}
